using System;
using CodeAngel.Domain.Framework;
using CodeAngel.Domain.Aggregates;
using System.Threading.Tasks;

namespace CodeAngel.Domain
{
	public partial class User : IUser
	{
		#region SetDefaultPropertyValues

        /// <summary>
        /// Sets the properties of a new entity to its default values.
        /// </summary>
		internal override void SetDefaultPropertyValues()
		{
            this.SetFriendlyIdAsync();
            this.UserID = Guid.NewGuid();
		}

		#endregion

        #region LoggedInUser

        /// <summary>
        /// Gets the currently logged in user.
        /// </summary>
        public static User LoggedInUser { get { return _loggedInUser; } }
        private static User _loggedInUser;

        /// <summary>
        /// Logs in the identified user.  Once done, the LoggedInUser will be set to this user.
        /// </summary>
        public async static Task<UserLogInAggregate> LogInUserAsync(string userName, string password)
        {
            // A User Name is required to log in.
            if (string.IsNullOrWhiteSpace(userName)) throw new LogInFailedException("Log in failed: User Name is required.", userName);

            var LogInAggregate = new UserLogInAggregate();
            await LogInAggregate.LoadAsync(userName, password);
            User._loggedInUser = LogInAggregate.Root;
            return LogInAggregate;
        }


        /// <summary>
        /// Logs out the currently logged in user.
        /// </summary>
        public static void LogOut()
        {
            _loggedInUser = null;
        }

        #endregion

		#region ToString

        /// <summary>
        /// Provides a user friendly string representation of this User.
        /// </summary>
		public override string ToString()
		{
            // TODO: identify a user friendly string reresentation of a User
            return base.ToString();
		}

		#endregion
	}
}

